/**
 * File Name   : AdminsCfgAction.java
 * Description : 
 */
package com.hh.admin.admuser.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.actions.MappingDispatchAction;

import com.hh.admin.admuser.form.AdminsCfgForm;
import com.hh.admin.admuser.form.ResultAdminsCfgBean;
import com.hh.admin.admuser.service.AdminsCfgService;
import com.hh.admin.infochg.service.InfoModifyService;

/**
 * 后台用户管理Action
 * @author 冯昌硕
 * @create 2012-12-20 下午8:57:16
 */
public class AdminsCfgAction extends MappingDispatchAction {

    private static final Logger log = Logger.getLogger(AdminsCfgAction.class);
    
    private AdminsCfgService adminsCfgService;
    
    private InfoModifyService infoModifyService;
    
    /**
     * 获取后台管理员用户信息列表
     * @create 2012-12-20 下午9:00:49
     */
    public ActionForward list(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        AdminsCfgForm adminsCfgForm = (AdminsCfgForm) form;
        
        // 查询当前页要显示的记录
        List<ResultAdminsCfgBean> adminsLst = adminsCfgService.getAdminsList(adminsCfgForm);
        request.setAttribute("adminLst", adminsLst);
        
        // 查询总记录数
        int totleCnt = adminsCfgService.getAdminsTotleCnt(adminsCfgForm);
        adminsCfgForm.setTotleCnt(totleCnt);
        
        // 计算记录总页数
        int pageCnt = 1;
        pageCnt = totleCnt / adminsCfgForm.getPageSize();
        if(totleCnt % adminsCfgForm.getPageSize() != 0) {
            pageCnt += 1;
        }
        adminsCfgForm.setTotlePage(pageCnt);
        log.debug("查询管理用户记录：totleCnt=[" + totleCnt + "] pageCnt=[" + pageCnt + "]");
        
        return mapping.findForward("view");
    }
    
    /**
     * 删除单个指定管理员用户记录
     * @create 2012-12-23 下午6:17:34
     */
    public ActionForward deleteOne(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        AdminsCfgForm adminsCfgForm = (AdminsCfgForm) form;
        
        String account = adminsCfgForm.getAccount();
        log.debug("管理员用户删除，要删除的账号=[" + account + "]");
        int delCnt = adminsCfgService.deleteOneAdminUser(account);
        log.debug("管理员用户[" + account + "]删除，删除影响行数=[" + delCnt + "]");
        
        return mapping.findForward("view");
    }
    
    /**
     * 批量删除管理员用户
     * @throws Exception
     * @create 2012-12-23 下午9:27:58
     */
    public ActionForward deleteMany(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        
        AdminsCfgForm adminsCfgForm = (AdminsCfgForm) form;
        String[] accounts = adminsCfgForm.getAccounts();
        
        int delCnt = adminsCfgService.deleteManyAdminUser(accounts);
        log.debug("批量删除，影响行数:[" + delCnt + "]");
        
        return mapping.findForward("view");
    }
    
    /**
     * 添加新用户
     * @create 2012-12-22 下午1:57:32
     */
    public ActionForward add(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        AdminsCfgForm adminsCfgForm = (AdminsCfgForm) form;
        
        boolean unique = adminsCfgService.uniqueAccount(adminsCfgForm);
        if(!unique) {
            // 用户名不惟一
            ActionMessage errorMsg = new ActionMessage("admin.error.exist");
            ActionErrors errors = new ActionErrors();
            errors.add("view.admin.error.exist", errorMsg);
            addErrors(request, errors);
            
            return mapping.findForward("faild");
        }
        
        adminsCfgService.insertAdminUser(adminsCfgForm);
        
        return mapping.findForward("success");
    }
    
    /**
     * 跳转到管理员编辑页面
     * @create 2012-12-24 下午8:06:53
     */
    public ActionForward detailForEdit(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        AdminsCfgForm adminsCfgForm = (AdminsCfgForm) form;
        String account = adminsCfgForm.getAccount();
        ResultAdminsCfgBean result = adminsCfgService.getAdminDetail(account);
        
        request.setAttribute("adminDetail", result);
        
        return mapping.findForward("edit");
    }
    
    /**
     * 编辑管理员用户信息
     * @create 2012-12-24 下午8:31:08
     */
    public ActionForward edit(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        AdminsCfgForm adminsCfgForm = (AdminsCfgForm) form;
        String account = adminsCfgForm.getAccount();
        String newPwd = adminsCfgForm.getPassword();
        int updCnt = infoModifyService.updatePwd(account, newPwd);
        log.debug("修改用户[" + account + "]信息，修改影响行数=[" + updCnt + "]");
        return mapping.findForward("success");
    }

    public void setAdminsCfgService(AdminsCfgService adminsCfgService) {
        this.adminsCfgService = adminsCfgService;
    }

    public void setInfoModifyService(InfoModifyService infoModifyService) {
        this.infoModifyService = infoModifyService;
    }
}
